from machine import Pin, I2C
from lib.sh1106 import SH1106, SH1106_I2C
import utime
import time
from setting import *
 
i2c = I2C(scl=Pin(POWER_PIN_OLED_I2C_SCL), sda=Pin(POWER_PIN_OLED_I2C_SDA), freq=600000)
oled = SH1106_I2C(128, 64, i2c, None, 0x3c)

            
            
 # Screen size
width=128
height=64 # we could make this be 63 but the init method should use the full value
# oled = SSD1306_I2C(width, height, i2c)
 
# 
# oled.fill(0) # clear to black
# oled.text('MicroPython!', 10, 10, 1)
# 
# a few register definitions
_SET_CONTRAST        = const(0x81)
_SET_NORM_INV        = const(0xa6)
_SET_DISP            = const(0xae)
_SET_SCAN_DIR        = const(0xc0)
_SET_SEG_REMAP       = const(0xa0)
 

oled.fill(0)
# oled.poweroff()
# oled.poweron()
# oled.invert(1)
# oled.write_cmd(0xAF)
# byt=bytearray(128*1)
# byt[0]=0x01

_LOW_COLUMN_ADDRESS  = const(0x00)
_HIGH_COLUMN_ADDRESS = const(0x10)
_SET_PAGE_ADDRESS    = const(0xB0)

dinosaur=[0x03,0x01,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x7F,0xFF,0x8F,0xAF,0x8D,0xF9,0x78,
0xC0,0xE0,0xF0,0x78,0x7F,0xFD,0xF0,0xFC,0xFF,0xF9,0xF0,0xE0,0xC0,0x00,0x80,0x00]

def OLED_Init():
     oled.write_cmd(0xAE) #display off
     oled.write_cmd(0x20) #Set Memory Addressing Mode
     oled.write_cmd(0x10) #00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
     oled.write_cmd(0xb0) #Set Page Start Address for Page Addressing Mode,0-7
     oled.write_cmd(0xc8) #Set COM Output Scan Direction
     oled.write_cmd(0x00) #---set low column address
     oled.write_cmd(0x10) #---set high column address
     oled.write_cmd(0x40) #--set start line address
     oled.write_cmd(0x81) #--set contrast control register
     oled.write_cmd(0xff) #???? 0x00~0xff
     oled.write_cmd(0xa1) #--set segment re-map 0 to 127
     oled.write_cmd(0xa6) #--set normal display
     oled.write_cmd(0xa8) #--set multiplex ratio(1 to 64)
     oled.write_cmd(0x3F) #
     oled.write_cmd(0xa4) #0xa4,Output follows RAM content;0xa5,Output ignores RAM content
     oled.write_cmd(0xd3) #-set display offset
     oled.write_cmd(0x00) #-not offset
     oled.write_cmd(0xd5) #--set display clock divide ratio/oscillator frequency
     oled.write_cmd(0xf0) #--set divide ratio
     oled.write_cmd(0xd9) #--set pre-charge period
     oled.write_cmd(0x22) #
     oled.write_cmd(0xda) #--set com pins hardware configuration
     oled.write_cmd(0x12)
     oled.write_cmd(0xdb) #--set vcomh
     oled.write_cmd(0x20) #0x20,0.77xVcc
     oled.write_cmd(0x8d) #--set DC-DC enable
     oled.write_cmd(0x14) #
     oled.write_cmd(0xaf) #--turn on oled panel

 
def OLED_Set_Pos(x, y):
     oled.write_cmd(_SET_PAGE_ADDRESS + y)
     oled.write_cmd((((x+2)&0xf0)>>4)+0x10)
     oled.write_cmd( (x+2)&0x0f)

def OLED_demoShow():
    byt=bytearray(1)
    byt[0]=0xff
    OLED_Set_Pos(5,0)
    oled.write_data(byt)
    oled.write_data(byt)
    byt[0]=0x0C
    oled.write_data(byt)
    byt[0]=0x12
    oled.write_data(byt)
    byt[0]=0x21
    oled.write_data(byt)
    byt[0]=0x42
    oled.write_data(byt)
    byt[0]=0x84
    oled.write_data(byt)
    byt[0]=0x42
    oled.write_data(byt)
    byt[0]=0x21
    oled.write_data(byt)
    byt[0]=0x12
    oled.write_data(byt)
    byt[0]=0x0c
    oled.write_data(byt)
    oled.p16(dinosaur,0,48)
    oled.show()
    
# _LOW_COLUMN_ADDRESS  = const(0x00)
# _HIGH_COLUMN_ADDRESS = const(0x10)
# oled.write_cmd(_SET_PAGE_ADDRESS | 0)
# oled.write_cmd(_LOW_COLUMN_ADDRESS | 2)
# oled.write_cmd(_HIGH_COLUMN_ADDRESS | 0)     
# oled.write_data(byt)
# OLED_Init()

# oled.write_cmd(_SET_PAGE_ADDRESS | 2)
# oled.write_cmd(_LOW_COLUMN_ADDRESS | 2)
# oled.write_cmd(_HIGH_COLUMN_ADDRESS | 0)     
# oled.write_data(byt)
# OLED_Set_Pos(3,0)
 
OLED_demoShow()
 
# #         time.sleep(0.001

 
print('done')
